package org.noootsab.gae.games.tzaar.service.impl;

import com.google.appengine.api.datastore.*;
import org.noootsab.gae.games.tzaar.model.*;
import org.noootsab.gae.games.tzaar.service.*;

import javax.jdo.*;
import javax.jdo.Query;

/**
 * User: sandy
 * Date: 13-avr.-2010
 * Time: 21:31:28
 */
public class StatisticsServiceImpl implements StatisticsService {
    public Statistic getPlayerStats(String playerKey) {
        PersistenceManager persistenceManager = PersistenceManagerFactoryHelper.get().getPersistenceManager();
        try {
            Query query = persistenceManager.newQuery(Statistic.class, "player == :p");
            query.setUnique(true);

            Key pKey = KeyFactory.stringToKey(playerKey);
            Statistic stat = (Statistic) query.execute(pKey);
            if (stat == null) {
                stat = new Statistic();
                stat.setPlayer(pKey);
                stat = persistenceManager.makePersistent(stat);
            }
            return stat;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            persistenceManager.close();
        }
        return null;
    }
}
